#include "Calculate.h"

float Calculate_Virtual(uint16_t *dat,float average,uint16_t len,float_t zero,float_t sens)
{
	uint16_t i=0;
	float Virtual=0.0f;
	for(i = 0; i < len; i++)
	{
		Virtual += (float_t)pow((dat[i] * 0.01f - zero) * sens - average,2);
	}
	return sqrt(Virtual/len);
}

void Calculate_Eigenvalue(uint16_t *data,uint16_t *Vdata,uint16_t *Sdata,vibrateConstant_t *Virtul,
		Smsgs_vibrateChannelConfig_t st_channelconfg,uint16_t len)
{
	float_t avg = 0,avgv=0,avgs=0;
	float_t dat = 0, Sdat = 0;

	float_t Smax = (float_t)((Sdata[0] - st_channelconfg.DISP_Zero) * 0.01f) * (st_channelconfg.DISP_Sens * 0.01f);//位移最大最小值
	float_t Smin =  Smax;

	Virtul->max = (float_t)((data[0] - st_channelconfg.ACC_Zero) * 0.01f) * (st_channelconfg.ACC_Sens * 0.01f);
	Virtul->min = Virtul->max;
	for(uint16_t i = 0; i < len; i++){
		dat = (float_t)((data[i] - st_channelconfg.ACC_Zero) * 0.01f) * (st_channelconfg.ACC_Sens * 0.01f);
		avg += dat;
		Virtul->max = (Virtul->max > dat ? Virtul->max : dat);
		Virtul->min = (Virtul->min > dat ? dat : Virtul->min);
		//速度
		avgv += (float_t)((Vdata[i] - st_channelconfg.Speed_Zero) * 0.01f) * (st_channelconfg.Speed_Sens * 0.01f);
		//位移
		Sdat = (float_t)((Sdata[i] - st_channelconfg.DISP_Zero) * 0.01f) * (st_channelconfg.DISP_Sens * 0.01f);
		Smax = (Smax > Sdat ? Smax : Sdat);
		Smin = (Smin > Sdat ? Sdat : Smin);
	}
	avg = avg / len;
	avgv = avgv / len;
	//avgs = avgs / len;
	Virtul->avg = avg;
	Virtul->max = Virtul->max - avg;
	Virtul->min = Virtul->min - avg;
	Virtul->vpp = Virtul->max - Virtul->min;
	Virtul->rms = Calculate_Virtual(data,avg,len,st_channelconfg.ACC_Zero * 0.01f,st_channelconfg.ACC_Sens * 0.01f);
	Virtul->speed = Calculate_Virtual(Vdata,avgv,len,st_channelconfg.Speed_Zero * 0.01f,st_channelconfg.Speed_Sens * 0.01f);
	Virtul->displance = Smax - Smin;
}

float Calculate_Virtual_OneData(float dat,float average)
{
	uint16_t i=0;
	float Virtual=0.0f;
	Virtual = (dat - average)*(dat - average);
	return Virtual;
}

void Calculate_Vpp_Max_Min_Avg_OneData(float dat,float *max,float_t *min,float_t *vpp,float_t *avg)
{
	*max = *max > dat ? *max : dat;
	*min = *min > dat ? dat : *min;
	*vpp = (*max - *min);
	*avg += dat;
}

float_t Calculate_SumVirtual(float SumSquares,uint32_t len)
{
	return sqrt(SumSquares/len);
}

float_t Calculate_Avg_OneData(float SUM, uint32_t len)
{
	return (SUM / len);
}